package com.haojii.notifier.user;

import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.haojii.notifier.helper.PMF;

public class UserUtils {

	private static final Logger log = Logger.getLogger(UserUtils.class.getName());
	
	public static List<User> list()
	{
		PersistenceManager pm = PMF.get().getPersistenceManager();
	
		Query query = pm.newQuery(User.class);
		List<User> es = (List<User>) query.execute();
		List<User> result = (List<User>)pm.detachCopyAll(es);
		pm.close();
		return result;
	}
	
	public static User get(String email)
	{
		User e=null,detached = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
		e = pm.getObjectById(User.class, email);
		detached = pm.detachCopy(e);
		}
		catch(Exception ex)
		{
			
		}
		finally{
			pm.close();
		}
		return detached;
	}
	
	public static void save(User user)
	{
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.makePersistent(user);
		pm.close();
	}
	
	public static void update(User user)
	{
		PersistenceManager pm = PMF.get().getPersistenceManager();
		User dest = pm.getObjectById(User.class, user.getEmail());
		if(dest != null)
		{
			pm.makePersistent(user);
		}
		pm.close();
	}
	
	public static void delete(User user)
	{
		PersistenceManager pm = PMF.get().getPersistenceManager();
		User dest = pm.getObjectById(User.class, user.getEmail());
		if(dest != null)
		{
			pm.deletePersistent(user);
		}
		pm.close();
	}
	
	
}
